/* Copyright 2009 Chitrang Desai
This file is part of Java's Kademlia Extension (JKADX).

JKADX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

JKADX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JKADX.  If not, see <http://www.gnu.org/licenses/>.
*/

package jkadx.dht;

import jkadx.core.RPC;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
 *
 * @author Desai
 */
public class TestUniqueId
{   
    @Test
    public void test1()
    {
        UniqueId zero  = new UniqueId("00");
        UniqueId one   = new UniqueId("01");
        UniqueId two   = new UniqueId("10");
        UniqueId target;
        int result;

        // 00 ^ 10 = 10
        // 01 ^ 10 = 11
        target = two;
        result = one.compareTo(zero, target);
        assertEquals("Failed to return correct comparison of distance to target.", result, 1);

        target = one;
        result = two.compareTo(zero, target);
        // 10 ^ 01 = 11
        // 00 ^ 01 = 01
        assertEquals("Failed to return correct comparison of distance to target.", result, 1);

        target = one;
        result = two.compareTo(one, target);
        // 10 ^ 01 = 11
        // 01 ^ 01 = 00
        assertEquals("Target was the same so", result, 1);
    }

    @Test
    public void Test2()
    {
        //random UniqueId Testing
        for (int i = 0; i<= 100; i++)
        {
        UniqueId uniqueid1 = UniqueId.newRandom();
        UniqueId uniqueid2 = UniqueId.newRandom();

        assertEquals ("Failed to get different uniqueId everytime", false, uniqueid1==uniqueid2);
        }


        UniqueId Id1= new UniqueId("000");
        UniqueId Id2= new UniqueId("010");
        UniqueId Id3= new UniqueId("011");

        //MSB Testing
        int i= UniqueId.getMSBAfterXoring(Id1, Id2);
        int j =158;

        assertEquals ("Failed to return MSB after xoring",i, j);

        //Distance Testing
        //000 ^ 010 = 010
        //011 ^ 010 = 001
        int result= Id1.compareTo(Id2, Id3);
        //System.out.print("result"+" "+result);
        assertEquals ("Failed to return correct comparison",result, 1);

        //010 ^ 001= 011
        //001 ^ 001= 000
        int result2= Id2.compareTo(Id1, Id1);
        //System.out.print("result2"+" "+result2);
        assertEquals ("Failed to return correct comparison",result2, 1);
    }

    // Anthony Surma
    @Test
    public void testInsert()
    {
        for (int i = 0; i < 100000; i++)
        {
            byte[] bytes = new byte[RPC.HEADER_LENGTH];
            UniqueId rpcId = UniqueId.newRandom();

            rpcId.insert(bytes, RPC.ID_OFFSET);
            UniqueId test = new UniqueId(bytes, RPC.ID_OFFSET);
            assertEquals("Failed insert and comparison on empty array", test, rpcId);
        }
        for (int i = 0; i < 100000; i++)
        {
            byte[] bytes = new byte[UniqueId.NBITS / Byte.SIZE];
            UniqueId rpcId = UniqueId.newRandom();

            UniqueId.newRandom().insert(bytes, 0);
            rpcId.insert(bytes, 0);
            UniqueId test = new UniqueId(bytes, 0);
            assertEquals("Failed insert and comparison on non-empty array", test, rpcId);
        }



    }

    public static void main(String[] args)
    {
	org.junit.runner.JUnitCore.main(TestUniqueId.class.getName());
    }
}